<?php

namespace app\modules\order\controllers;

use Yii;
use yii\web\Controller;
use common\models\Tools;
use common\models\Order;

/**
 * 确认收货
 */
class ConfirmController extends Controller
{
    public $enableCsrfValidation = false;
    /***
     * 创建订单
     */
    public function actionIndex(){
        
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = "未登录";
            $output['errorcode'] = 1001;
            Tools::outputjson($output);
        }
        
        $order_no = Tools::GetSafeParam('order_no', '', 1);
        
        $userid = Yii::$app->user->identity->id;
        
        $order = Order::findOne(['order_no'=>$order_no]);
        
        if(empty($order)){
            $output['success'] = 0;
            $output['msg'] = "订单不存在！";
            $output['errorcode'] = 1002;
            Tools::outputjson($output);
        }
        
        if($order->buyer_id != $userid){
            $output['success'] = 0;
            $output['msg'] = "没有权限！";
            $output['errorcode'] = 1003;
            Tools::outputjson($output);
        }
        
        $result = Order::orderConfirm($order_no);
        
        if($result){
            $output['success'] = 1;
            $output['msg'] = '确认收货成功！';
            Tools::outputjson($output);
        }else{
            $output['success'] = 0;
            $output['msg'] = '确认收货失败！';
            Tools::outputjson($output);
        }
        
    }
}
